<?php
# genereate PHP objects and AS Objects

/**
 *Simple CRUD Code Generator
 *Email:  support@crazedcoders.com
 *Website: http://www.crazedcoders.com/
 *Copyright (C) 2007 CrazedCoders
 *
 *This program is free software: you can redistribute it and/or modify
 *it under the terms of the GNU General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * returns an array where the first element is the php object file string
 * and the second element is the as object file string
 */
//function generateVOs( $tablename, $columns, $package ) {
function generateVOs( $table ) {
	//var_dump( $table );
	$tablename = toCamelCase( $table[0][2]['table_name'] );
	//$columns = getTableColumns( $tablename );
	$columns = $table;
	$className = toCamelCase( $tablename );

	$fkeys = getFKeys( $table );

	//echo "\n\n\ncolumns\n"; var_dump( $columns);

	$package = con::$package;

	$manyTables = findManyToManyForTable( $table );
	$refManyTables = findRefManyColumnsFromTable( $table );
	//echo "refManyTables\n";
	//var_dump( $refManyTables );

	$export =
"<?php
/**
 *Simple CRUD Code Generator
 *Email:  support@crazedcoders.com
 *Website: http://www.crazedcoders.com/
 *Copyright (C) 2007 CrazedCoders
 *
 *This program is free software: you can redistribute it and/or modify
 *it under the terms of the GNU General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *THE PROGRAM IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class $tablename {
		public function __construct() {
			//\$this->auniq = uniqid();
		}
";

			$exportAS =
"package $package.vo
{
	/**
	 *Simple CRUD Code Generator
	 *Email:  support@crazedcoders.com
	 *Website: http://www.crazedcoders.com/
	 *Copyright (C) 2007 CrazedCoders
	 *
	 *This program is free software: you can redistribute it and/or modify
	 *it under the terms of the GNU General Public License as published by
	 *the Free Software Foundation, either version 3 of the License, or
	 *(at your option) any later version.
	 *
	 *THE PROGRAM IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	 *SOFTWARE.
	 *
	 *You should have received a copy of the GNU General Public License
	 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 */

	import com.adobe.cairngorm.vo.IValueObject;
	import $package.event.*;


	[RemoteClass(alias='$package.$tablename')]
	[Bindable]
	public class $tablename implements IValueObject
	{

";

		//var_dump( $columns );

			$exportSetState =
'	public function __set_state( $assoc ) {
';

			$exportASConstructPart1 =
'
		public static function construct(
';

			$exportASConstructPart2 =
'
				):'.$className.' {
			var ret:'.$className.' = new '.$className.'();
';

			$exportASClone =
"
		public function clone():$tablename {
			var clone:$tablename = new $tablename();
";

			$exportASCloneLight =
"
		public function store():$tablename {
			var clone:$tablename = new $tablename();
";

			$exportASCloneReverse =
"
		public function restore(clone:$tablename):void {
			//var clone:$tablename = new $tablename();
";

			$firstflag = true;
			foreach( $columns as $key=>$columnassoc ) {
				if( ! is_int( $key ) ) {
					continue;
				}
				$column = toCamelCaseMember( $columnassoc[0] );
				$type = $columnassoc[1];
				$fkeyname = toFKeyName( $column );
				$isFkey =
						array_key_exists(2, $columnassoc)
					   	&& $columnassoc[2]['constraint_type'] == 'FOREIGN KEY'
						&& $fkeyname;
				if( ! $isFkey ) {
					$export .=
"	var \$$column;
";

					$exportASConstructPart2 .=
'			ret.'.toCamelCaseMember($column).' = '.toCamelCaseMember($column).';
';

					$exportASClone .=
"			clone.$column = this.$column;
";

					$exportASCloneLight .=
"			clone.$column = this.$column;
";

					$exportASCloneReverse .=
"			this.$column = clone.$column;
";

					// If $firstflag == true, no need for a ',' to deliminate
					// the first argument, otherwise preceed arguments with a ','
					if( $type == "tinyint" || $type == "bit") {
						$exportASConstructPart1 .=
'				'.($firstflag?'':',').toCamelCaseMember($column).':Boolean
';
						$exportAS .=
"		public var $column:Boolean = false;
";
						$exportSetState .=
"		if( \$assoc['$column'] == null ) {
			\$this->$column = false;
		}
		else {
			\$this->$column = (bool)\$assoc['$column'];
		}
";
					}

					else if( strrpos($type,"int") > -1 || $type == "year" ) {
						$exportASConstructPart1 .=
'				'.($firstflag?'':',').toCamelCaseMember($column).':int
';
						$exportAS .=
"		public var $column:int = 0;
";
						$exportSetState .=
"		if( \$assoc['$column'] == null ) {
			\$this->$column = 0;
		}
		else {
			\$this->$column = (int)\$assoc['$column'];
		}
";
					}

				else if( $type == "date" ) {
						$exportASConstructPart1 .=
'				'.($firstflag?'':',').toCamelCaseMember($column).':Date
';
						$exportAS .=
"		public var $column:Date = new Date();
";
						$exportSetState .=
"		if( \$assoc['$column'] == null ) {
			\$this->$column = null;
		}
		else {
			\$this->$column = (string)\$assoc['$column'];
		}
";
						}

				else if( $type == "decimal" || $type == "float" || $type == "numeric"
							|| $type == "real" || $type == "double") {
						$exportASConstructPart1 .=
'				'.($firstflag?'':',').toCamelCaseMember($column).':Number
';
						$exportAS .=
"		public var $column:Number = 0;
";
						$exportSetState .=
"		if( \$assoc['$column'] == null ) {
			\$this->$column = 0;
		}
		else {
			\$this->$column = (float)\$assoc['$column'];
		}
";
						}

					else if( strrpos($type,"char") > -1 ||  strrpos($type,"text") > -1
							|| strrpos($type, "time") > -1 ) {
						$exportASConstructPart1 .=
'				'.($firstflag?'':',').toCamelCaseMember($column).':String
';
						$exportAS .=
"		public var $column:String = \"\";
";
						$exportSetState .=
"		\$this->$column = (string)\$assoc['$column'];
";
					}
					else {
						// Ignore all other column types.

					}

				}
				else {
					//echo "fkey inside if\n";
					$refClassname = toCamelCase( $columnassoc[2]
							['referenced_table_name'] );
					$refPKey = getPKey( con::$tables[$columnassoc[2]
							['referenced_table_name']] );
					$export .=
"	var \$$column;
";
					$export .=
'	var $'.$fkeyname.';
';

					if( $type == "tinyint" || $type == "bit")
					{
						$exportAS .=
"		public var $column:Boolean = false;	// used only when object is sent
											// to be saved or created or
											// deleted

";

						$exportSetState .=
"		if( \$assoc['$column'] == null ) {
			\$this->$column = 0;
		}
		else {
			\$this->$column = (int)\$assoc['$column'];
		}
";
						}
					else if( strrpos($type,"int") > -1 || $type == "year" )
					{
						$exportAS .=
"		public var $column:int = 0;		// used only when object is sent
											// to be saved or created or
											// deleted

";
						$exportSetState .=
"		if( \$assoc['$column'] == null ) {
			\$this->$column = 0;
		}
		else {
			\$this->$column = (int)\$assoc['$column'];
		}
";
					}
					else {
						$exportAS .=
"		public var $column:String = \"\";	// used only when object is sent
											// to be saved or created or
											// deleted
";
						$exportSetState .=
"		\$this->$column = (string)\$assoc['$column'];
";
					}



					$exportSetState .=
'		$this->'.$fkeyname.' = $assoc[\''.$fkeyname.'\'];
';
					$exportAS .=
'		public var '.$fkeyname.':'.$refClassname.' = null;
';
						$exportASConstructPart1 .=
'				'.($firstflag?'':',').$fkeyname.':'.$refClassname.'
';
					$exportASConstructPart2 .=
'			ret.'.$fkeyname.' = '.$fkeyname.';
';

					$exportASClone .=
"			clone.$fkeyname = this.$fkeyname ;
";
					$exportASCloneLight .=
"			if( this.".$fkeyname." != null ) {
				clone.$column = this."
					.$fkeyname.".$refPKey;
			}
";
					$exportASCloneReverse .=
"			if( clone.".$fkeyname." != null ) {
				this.$column = clone."
					.$fkeyname.".$refPKey;
			}
";

					$exportASCloneReverse .=
"			this.$fkeyname = clone.$fkeyname ;
";

/*
		private var _'.$fkeyname.'_loaded:Boolean = false;
		[Bindable(event=\''.$fkeyname.'Changed\')]
		public function get '.$fkeyname.'():'.$refClassname.' {
			if( _'.$fkeyname.'_loaded ) {
				return( _'.$fkeyname.' );
			}
			var item:'.$refClassname.' =
					'.$refClassname.'Model.getInstance().getById('.$column.');
			if ( item )
					) {
				_'.$fkeyname.' = item;
				return( item );
			}
			else {
				var ev:'.$refClassname.'Event = new '.$refClassname.'Event(
						'.$refClassname.'Event.'.strtoupper($refClassname).
						'_ACTION_GET_ONE );
				ev.paramPrimaryKey = this.'.$column.';
				ev.dispatch();
				var vcontrol:ViewController = new ViewController();
				vcontrol.eventFilter = [ '.$refClassname.'Event.'
					.strtoupper($refClassname).'_ACTION_GET_ONE ] ;
				vcontrol.addEventListener("updated", on'
						.toCamelCase($fkeyname).'Updated );
		  	}
			return( null );
		}
		private function on'
						.toCamelCase($fkeyname).'Updated():void {
		}

';
*/
				}

				if( array_key_exists( 3, $columnassoc ) ) {
					foreach( $columnassoc[3] as $k=>$refColumn ) {
						if(
							array_key_exists( 'isManyToMany', con::$tables[
								$columnassoc[3][$k]['table_name']] )
							&&  con::$tables[$columnassoc[3][$k]['table_name']]
								['isManyToMany']) {
							continue;
						}
						$refColName = toCamelCaseMember(
								$refColumn['table_name'] );
						$export .=
'	var $'.$refColName.'s;
';
						$exportSetState .=
'		$this->'.$refColName.'s = $assoc[\''.$refColName.'s\'];
';
						$exportAS .=
'		public var '.$refColName.'s:Array= null;
';
					}
				}

				$firstflag = false;
			}
			//echo "tablename : $tablename\n";
			//echo "refManyTables : ";
			//var_dump( $refManyTables );
			$export .=
"	// many to many arrays
";
			foreach( $refManyTables as $k=>$refManyColumn ) {
				if( ! is_int( $k ) ) {
					continue;
				}
				$refTableName =
					toCamelCaseMember(
							$refManyColumn['away_column'][2]
							['referenced_table_name']);
				//echo "refTableName:$refTableName\n";
				$export .=
'	var $'.$refTableName.'s;
';

				$exportSetState .=
'		if( array_key_exists( '.$refTableName.'s, $assoc )) {
			$this->'.$refTableName.'s = $assoc[\''.$refTableName.'s\'];
		}
';
				$exportAS .=
'		public var '.$refTableName.'s:Array = null ;
';
				$exportASConstructPart1 .=
'				'.($firstflag?'':',').$refTableName.'s:Array
';
				$exportASConstructPart2 .=
'			ret.'.$refTableName.'s = '.$refTableName.'s;
';
				$exportASClone .=
'			clone.'.$refTableName.'s = this.'.$refTableName.'s;
';
				$exportASCloneReverse .=
'			this.'.$refTableName.'s = clone.'.$refTableName.'s;
';


				$firstflag = false;
			}

			$exportSetState .=
"	}
";

			$export .=
"	var \$_explicitType = \"$package.$tablename\";
";
			$export .= $exportSetState;
			$export .=
"}
?>";


			$exportAS .= $exportASConstructPart1 . $exportASConstructPart2 .
"
			return ret;
		}
";

			$exportASClone .=
"			return( clone );
		}
";
			$exportASCloneLight .=
"			return( clone );
		}
";
			$exportASCloneReverse .=
"
		}
";
			$exportAS .= $exportASClone;
			$exportAS .= $exportASCloneLight;
			$exportAS .= $exportASCloneReverse;
			$exportAS .=
"	}
}
";
			return( array( $export, $exportAS ) );

		}

?>
